//
//  PersonViewController.swift
//  maosheng
//
//  Created by lxl on 2019/8/15.
//

import UIKit

class PersonViewController: RootViewController {
    let personView = PersonView()
    var userId : String?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
//        personView.dataSource.addObjects(from: arr)
        self.fd_prefersNavigationBarHidden = true

        view.addSubview(personView)
        personView.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }
//        personView.didClickRow = {[weak self] (row,title) in
//            if row == 7 || row == 8{
//              let web = PublicWebViewController()
//                web.webURLStr = row == 7 ? contact : cooperation
//                self?.navigationController?.pushViewController(web, animated: true)
//              return
//            }
//            self?.jumpTheResponseVC(tagetVC: sourceVCs[row], title: title!)
//        }
//        personView.tableView.reloadData()
        personView.topBtn.addTarget(self, action: #selector(jumpPersonInfo), for: .touchUpInside)
        let sourceVCs = ["MineAttentionController","MineFansController","MyCollectViewController","MyBrowseController","MyCommentController","MyLikeController","EditPersonInfoController","",""]
        let arr = ["我的关注","我的粉丝","我的收藏","最近浏览","我的评论","我的点赞","编辑资料","我的客服","商务合作"]

        personView.didClickBtn = {[weak self] (tag) in
            switch tag {
            case 201,202,203,204:
                let index = tag - 201
                self?.jumpTheResponseVC(tagetVC: sourceVCs[index], title: arr[index])
            case 301,302:
                let index = tag - 301 + 4
                self?.jumpTheResponseVC(tagetVC: sourceVCs[index], title: arr[index])
            case 401,402:
                let web = PublicWebViewController()
                web.webURLStr = tag == 401 ? contact : cooperation
                web.str_title = tag == 401 ? "我的客服" : "商务合作"
                self?.navigationController?.pushViewController(web, animated: true)
            case 404:
                self?.jumpTheResponseVC(tagetVC: "SettingViewController", title: "设置")
            default:
                print("")
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
//        self.navigationController?.navigationBar.isHidden = true
        getMineInfo()
    }
//    override func viewWillDisappear(_ animated: Bool) {
//        self.navigationController?.navigationBar.isHidden = false
//    }
    
    //MARK:-获取个人中心接口
    func getMineInfo() -> Void {
        let url =  "api/services/app/Session/GetCurrentLoginInformations"
        SDataCenter.shared.SGetJsonDataWithUrl(str_url: url, params: nil) { (responseData, isSuccess) in
            
            if isSuccess && responseData is [String : Any]{
                if let model = PersonModel.deserialize(from: responseData as? [String : Any]){
                    self.personView.setPersonViewInfo(model: model)
                    self.userId = model.user?.userId
                }
                
                
            }else{
                
                
            }
        }
    }
    
    @objc func jumpPersonInfo() -> Void {
        let info = PersonInfoController()
        info.userId = self.userId
        self.navigationController?.pushViewController(info, animated: true)
    }

}
